Destroying the Resources

Destroy all the resources that we've created in AWS.

Destroying the resources#

We’re almost finished with the quick exploration of Terraform using EKS as an example. We saw how to add and change resources, and the only thing missing is to learn how to destroy them.

If we’d like to delete some of the resources, all we have to do is remove their definitions and execute terraform apply. However, in some cases, we might want to destroy everything. There’s a command for that as well.

Command for destroying the resources

At the end of the process, we might see an error stating that it can’t delete the bucket with Terraform state without force_destroy set to true. Don’t be alarmed. That’s normal. After Terraform destroyed everything, it tried to destroy the bucket where we keep the state. However, we didn’t specify that the bucket can be removed if it contains files. The process failed to remove that bucket, and that’s a good thing. That will allow us to recreate the same cluster in the sections that follow.

The cluster and all the other resources we defined are now gone. The exception is storage with the state that we left intact and that we’ll continue using in the exercises that follow.

Please note that we removed only the resources created through Terraform. Those that were created without it (e.g., keys) are still there. AWS won’t charge us anything (or much) for them, so unlike those we created with Terraform, there’s no good reason to remove them. On the other hand, we might want to use the definitions from this chapter to create a cluster that will be used for the exercises in the others. Keeping those created outside Terraform will simplify the process. All we’d have to do is execute terraform apply.

Prepare to destroy

Try it yourself#

You can try all of the commands used in this lesson in the code playground below. Press the “Run” button and wait for a few seconds for it to connect.

For ease of use, all of the commands above are combined in main.sh.

Please provide values for the following:
AWS_TF_VAR_state_bucket
Not Specified...
AWS_ACCESS_KEY_ID
Not Specified...
AWS_SECRET_ACCESS_KEY
Not Specified...
/
main.sh
terraform.tfstate
backend.tf
k8s-control-plane.tf
k8s-worker-nodes.tf
output.tf
provider.tf
storage.tf
variables.tf
Code playground

Reorganizing Definitions

Troubleshooting with Terraform